package com.yoofn.kotlin.av.view.activity

import android.view.KeyEvent
import android.widget.LinearLayout
import com.just.library.AgentWeb
import com.yanzhenjie.nohttp.rest.Response
import com.yoofn.kotlin.R
import com.yoofn.kotlin.base.BaseActivity
import com.yoofn.kotlin.base.Constants
import com.yoofn.kotlin.base.MyJavaScriptInterface
import com.yoofn.kotlin.base.ViewConfig
import com.yoofn.kotlin.databinding.ActivityWebviewBinding

/**
 *
 * Created by kalshen on 17/6/2.
 * @描述 展示web 界面主要的activity
 */
class WebActivity : BaseActivity<ActivityWebviewBinding, String>() {
    override fun setupConfig(): ViewConfig<String> {
        return ViewConfig(R.layout.activity_webview)
    }

    private var agentWeb: AgentWeb? = null
    override fun doInteractive() {

        with(contentView) {
            //配置agentWeb
            agentWeb = AgentWeb.with(this@WebActivity)
                    .setAgentWebParent(llWeb, LinearLayout.LayoutParams(-1, -1))
                    .useDefaultIndicator()
                    .defaultProgressBarColor()
                    .setReceivedTitleCallback { webView, s -> tvWebTitle.text = s }
                    .createAgentWeb()
                    .ready()
                    .go(intent.getStringExtra(Constants.Web.URL))
            agentWeb?.jsInterfaceHolder?.addJavaObject(Constants.Web.METHOD, MyJavaScriptInterface(this@WebActivity))
            ivWebBack.setOnClickListener {
                finish()
                overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right)
            }
        }
    }

    override fun <ResponseType> doBusiness(what: Int, response: Response<ResponseType>) {

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        agentWeb?.let { return it.handleKeyEvent(keyCode, event) }
        return super.onKeyDown(keyCode, event)
    }

    override fun onPause() {
        agentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

    override fun onResume() {
        agentWeb?.webLifeCycle?.onResume()
        super.onResume()
    }

}
